package com.admin.web;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.admin.model.FileMeta;
import com.admin.service.AdminCTSService;
import com.admin.service.AdminChooseService;
import com.admin.service.AdminOtherService;
import com.admin.service.AdminProjectService;
import com.admin.service.AdminSchoolService;
import com.admin.service.AdminTeachService;
import com.admin.util.AdminProjectExcelTool;
import com.admin.util.AdminScoreExcelTool;
import com.admin.util.AdminStudentExcelTool;
import com.common.model.AdminUser;
import com.common.model.ClassInfo;
import com.common.model.Course;
import com.common.model.Project;
import com.common.model.StudentUser;
import com.common.model.TeacherUser;
import com.common.model.TimePlan;
import com.common.util.Md5Encrypt;
import com.teacher.model.TeachCourse;

@Controller
@RequestMapping("/admin/teach")
public class AdminTeachWeb {
	@Autowired
	AdminSchoolService schooService;
	@Autowired
	AdminCTSService ctsService;
	@Autowired
	AdminChooseService choService;
	@Autowired
	AdminProjectService prjService;
	@Autowired
	AdminTeachService tchService;
	/**
	 * 查询所有管理员列表
	 * @param page 页码(从1开始)
	 * @return
	 */
	@RequestMapping("/showCourseList/{page}")
	public ModelAndView showCourseList(
			@PathVariable("page")int page,
			HttpSession session){
		ModelAndView mav=new ModelAndView();
		mav.setViewName("admin/teach/showCourseList");
		int courseCount=tchService.getAllCourseCount();
		int totalPage=(int)Math.ceil(courseCount/10.0);
		if(totalPage==0){
			totalPage=1;
		}
		if(page>totalPage){
			page=totalPage;
		}
		mav.addObject("page", page);
		mav.addObject("courseList",tchService.getAllCourse(page));
		mav.addObject("courseCount",courseCount );
		mav.addObject("totalPage",totalPage);
		mav.addObject("currentMenu", "showCourseList");
		return mav;
	}
	@ResponseBody
	@RequestMapping("/getCourseByCourseId/{course_id}")
	public  Course getCourseByCourseId(@PathVariable("course_id") int courseId){
		return tchService.getCourseByCourseId(courseId);
	}
	/**
	 * 修改课程
	 * @param course
	 * @return -1参数错误 -2 重复课程编号
	 */
	@ResponseBody
	@RequestMapping("/updateCourse")
	public int updateCourse(Course course){
		if(course.getId()==null||course.getName()==null||course.getName().trim().isEmpty()){
			return -1;
		}
		if(course.getCategory()==null||course.getCategory().trim().isEmpty()){
			return -1;
		}
		if(course.getCourseId()==null||course.getCourseId().trim().isEmpty()){
			return -1;
		}
		if(course.getCredit()==null||course.getTerm()==null||course.getWeeknum()==null){
			return -1;
		}
		ArrayList<Course> courseList=tchService.getAllCourseByCourseNo(course.getCourseId());
		if(courseList.size()>0){
			for (Course c : courseList) {
				if(c.getId()!=course.getId()){
					return -2;
				}
			}
		}
		tchService.updateCourse(course);
		return 0;
	}
	@ResponseBody
	@RequestMapping("/updateCoursePlan/{course_id}")
	public ArrayList<FileMeta> updateCoursePlan(@PathVariable("course_id")int courseId ,MultipartFile file){
		tchService.updateCoursePlan(courseId, file);
		FileMeta fileMeta=new FileMeta();
		fileMeta.setFileName(file.getOriginalFilename().toLowerCase());
		fileMeta.setFileSize(file.getSize()/1024.0+"kb");
		fileMeta.setFileType(file.getContentType());
		ArrayList<FileMeta> arr=new ArrayList<FileMeta>();
		arr.add(fileMeta);
		return arr;
	}
	@ResponseBody
	@RequestMapping("/insertCourse")
	public int insertCourse(Course course){
		if(course.getName()==null||course.getName().trim().isEmpty()){
			return -1;
		}
		if(course.getCategory()==null||course.getCategory().trim().isEmpty()){
			return -1;
		}
		if(course.getCourseId()==null||course.getCourseId().trim().isEmpty()){
			return -1;
		}
		if(course.getCredit()==null||course.getTerm()==null||course.getWeeknum()==null){
			return -1;
		}
		ArrayList<Course> courseList=tchService.getAllCourseByCourseNo(course.getCourseId());
		if(courseList.size()>0){
				return -2;
		}
		tchService.insertCourse(course);
		return 0;
	}	
	@ResponseBody
	@RequestMapping("/deleteCourse/{course_id}")
	public int deleteCourse(@PathVariable("course_id")int courseId){
		tchService.deleteCourse(courseId);
		return 0; 
	}
	/**
	 * 查询所有教学关系列表
	 * @param page 页码(从1开始)
	 * @return
	 */
	@RequestMapping("/showTeachCourseList/{clg_id}/{page}")
	public ModelAndView showTeachCourseList(
			@PathVariable("clg_id")int clgId,
			@PathVariable("page")int page,
			HttpSession session){
		ModelAndView mav=new ModelAndView();
		mav.setViewName("admin/teach/showTeachCourseList");
		mav.addObject("colleges", schooService.getAllCollege());
		mav.addObject("courseList",tchService.getAllCourseNoPage());
		AdminUser admin=(AdminUser)session.getAttribute("user");
		if(admin.isSuperAdmin()){
			int minClgId=schooService.getMinCollegeId();
			if(clgId<minClgId){
				clgId=minClgId;
			}
		}else{
			clgId=admin.getCollegeId();
		}
		int tcCount=tchService.getTeachCourseListCountByClgId(clgId);
		int totalPage=(int)Math.ceil(tcCount/10.0);
		if(totalPage==0){
			totalPage=1;
		}
		if(page>totalPage){
			page=totalPage;
		}
		mav.addObject("clgId", clgId);
		mav.addObject("page", page);
		mav.addObject("teachCourseList",tchService.getTeachCourseListByClgId(clgId, page));
		mav.addObject("tcCount",tcCount );
		mav.addObject("totalPage",totalPage);
		mav.addObject("currentMenu", "showTeachCourseList");
		return mav;
	}
	/**
	 * 
	 * @param tc
	 * @param session
	 * @return -1无权限 -2 参数错误
	 */
	@ResponseBody
	@RequestMapping("/insertTeachCourse")
	public int insertTeachCourse(TeachCourse tc,HttpSession session){
		if(tc.getTea_id()==null||tc.getCourse_id()==null||tc.getAddress()==null||tc.getAddress().trim().isEmpty()){
			return -2;
		}
		AdminUser admin=(AdminUser)session.getAttribute("user");
		if(!admin.isSuperAdmin()){
			TeacherUser tea=ctsService.getTeacherByTeacherId(tc.getTea_id());
			if(tea.getDepartment().getCollege().getId()!=admin.getCollegeId()){
				return -1;
			}
		}
		tchService.insertTeachCourse(tc);
		return 0;
	}
	/**
	 * 
	 * @param tcId
	 * @param session
	 * @return -1 无权限
	 */
	@ResponseBody
	@RequestMapping("/deleteTeachCourse/{tc_id}")
	public int deleteTeachCourse(@PathVariable("tc_id")int tcId,HttpSession session){
		TeachCourse tc=tchService.getTeachCourseByTcId(tcId);
		AdminUser admin=(AdminUser)session.getAttribute("user");
		int teaId=tc.getTeacher().getDepartment().getCollege().getId();
		if(!(admin.isSuperAdmin()||admin.getCollegeId()==teaId)){
			return -1;
		}
		tchService.deleteTeachCourse(tcId);
		return 0;
	}
}
